<template>
    <div>
        <div style="text-align: center; margin-top: 30px;">
            <span style="font-family: 隶书;font-size: 30px">修改密码</span>
        </div>
        <div style="max-width: 400px;margin: 0 auto;padding: 40px 20px;">
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm"  label-width="100px" class="demo-ruleForm">
                <el-form-item label="旧密码" prop="oldPass">
                    <el-input type="password" v-model="ruleForm.oldPass" placeholder="请输入旧密码" clearable size="small"/>
                </el-form-item>
                <el-form-item label="新密码" prop="pass">
                    <el-input type="password" v-model="ruleForm.pass" placeholder="请输入新密码" clearable size="small"/>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="ruleForm.checkPass" placeholder="请再次输入新密码" clearable size="small"/>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm">提交</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                    <el-button type="primary" @click="toHome">返回首页</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        data(){
            var oldPass = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('旧密码不能为空'));
                } else if (value.length<3){
                    return callback(new Error('密码不少于3位'));
                } else {
                    callback();
                }
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else if (value.length<3){
                    return callback(new Error('密码不少于3位'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value.length<3){
                    return callback(new Error('密码不少于3位'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    pass: '',
                    checkPass: '',
                    oldPass: ''
                },
                rules: {
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    oldPass: [
                        { validator: oldPass, trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            toHome(){
                this.$router.push("/home");
            },
            submitForm() {
                this.$refs['ruleForm'].validate((valid) => {
                    if (valid) {
                        this.$axios.post("system/api/user/updUserPassword/"+this.ruleForm.oldPass+"/"+this.ruleForm.pass).then(r=>{
                            if (r.data.t){
                                // this.$message.success(r.data.msg);
                                this.ruleForm={};
                                //对话框确认跳转首页
                                this.$confirm('密码修改成功是否回到系统首页?', '提示', {
                                    confirmButtonText: '确定',
                                    cancelButtonText: '取消',
                                    type: 'warning'
                                }).then(() => {
                                    this.$router.push("/Home")
                                }).catch(() => {
                                    this.$message({
                                        type: 'info',
                                        message: '已取消'
                                    });
                                });
                            }else {
                                this.$message.error(r.data.msg);
                                this.resetForm();
                                this.ruleForm={};
                            }
                        })
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style scoped>

</style>
